package com.guldan.jaina.manager;

import com.guldan.core.exception.BusinessException;
import com.guldan.core.manager.BaseManager;
import com.guldan.core.search.Pagination;
import com.guldan.core.utils.DateUtil;
import com.guldan.jaina.dto.grole.GRoleDTO;
import com.guldan.jaina.dto.grole.GRoleSearchDTO;
import com.guldan.jaina.utils.GAuthorityDomainConvertUtils;
import com.guldan.mapper.db.entity.GRole;
import com.guldan.mapper.db.entity.GRoleExample;
import com.guldan.mapper.db.mapper.GRoleMapperExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Silas.
 * Date: 2016/11/16
 * Time: 22:58
 */
@Service
public class GRoleManager extends BaseManager {

    @Autowired
    private GRoleMapperExt gRoleMapperExt;

    //获取单个角色信息
    public GRoleDTO get(Integer id) {
        return GAuthorityDomainConvertUtils.convert(gRoleMapperExt.selectByPrimaryKey(id));
    }

    //判断某个角色code是否已经存在
    private Boolean checkExist(String roleCode) {
        GRoleExample example = new GRoleExample();
        GRoleExample.Criteria criteria = example.createCriteria();
        criteria.andRoleCodeEqualTo(roleCode);
        criteria.andIsDeleteEqualTo(false);
        int count = gRoleMapperExt.countByExample(example);
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }

    //新增/更新单个角色，传入：GRoleDTO
    public GRoleDTO save(GRoleDTO dto) throws BusinessException {



        GRole entity;
        if (dto.getId() != null && dto.getId() > 0) {
            entity = gRoleMapperExt.selectByPrimaryKey(dto.getId());
            entity.setRoleName(dto.getRoleName());
            entity.setRoleCode(dto.getRoleCode());
            entity.setDisabled(dto.getDisabled());
            entity.setModifyTime(DateUtil.getCurrentTime());
            entity.setModifier(dto.getModifier());
            gRoleMapperExt.updateByPrimaryKeySelective(entity);
        } else {
            //判断是否存存在
            if (checkExist(dto.getRoleCode())) {
                throw new BusinessException("角色Code已存在");
            }
            entity = new GRole();
            entity.setRoleName(dto.getRoleName());
            entity.setRoleCode(dto.getRoleCode());
            entity.setDisabled(true);
            entity.setCreator(dto.getModifier());
            entity.setCreateTime(DateUtil.getCurrentTime());
            entity.setIsDelete(false);
            int ret = gRoleMapperExt.insertSelective(entity);
        }

        dto = GAuthorityDomainConvertUtils.convert(entity);
        return dto;
    }

    //删除单个权限信息
    public Boolean delete(Integer id) {
        GRole entity = gRoleMapperExt.selectByPrimaryKey(id);
        entity.setIsDelete(true);
        return gRoleMapperExt.updateByPrimaryKeySelective(entity) > 0;
    }

    //查询所有角色列表
    public List<GRoleDTO> list() {
        List<GRoleDTO> list = new ArrayList<>();
        GRoleExample example = new GRoleExample();
        GRoleExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteEqualTo(false);

        List<GRole> entityList = gRoleMapperExt.selectByExample(example);

        if (entityList != null && entityList.size() > 0) {
            for (GRole entity : entityList) {
                list.add(GAuthorityDomainConvertUtils.convert(entity));
            }
        }
        return list;
    }

}
